clear all;addpath('auxiliar'); addpath('Simulated_Datasets');load SP_SIMULATION_20k.mat
load Taxes_NoGov % FROM NO GOV EXTENSION

%% TAXES AT THE ERGODIC
TAUSIM(TAUSIM<0)=0;
%TAUSIM(TAUSIM>1)=1;
Tau_SIM_NOGOV(Tau_SIM_NOGOV<0)=0;




%% Optimal Policy in Equilibruim
mY=mean(priceSIM.*yNSIM+ySIM);
mean_ind=1;
bg=10;
shoc=13;
EBGp=sum(BG_bignext.*G_big,4);
Eq_bp=sum(G_big.*bp,4);
Eq_bmax=sum(G_big.*bpmax,4);
JTau1p=sum(G_big.*Tau1p,4);
bgrid_plot=bgrid1d+BGgrid(bg)*cte_debtfactor;

%FOR THE KINK
kink=20;
bgrid_plot1=zeros(NB+1,1);
EBGp1=zeros(NB+1,1);
Eq_bp1=zeros(NB+1,1);
Eq_bmax1=zeros(NB+1,1);
JTau1p1=zeros(NB+1,1);

bgrid_plot1(1:kink)=bgrid_plot(1:kink);
EBGp1(1:kink)=EBGp(bg,1:kink,shoc);
Eq_bp1(1:kink)=Eq_bp(bg,1:kink,shoc);
Eq_bmax1(1:kink)=Eq_bmax(bg,1:kink,shoc);
JTau1p1(1:kink)=JTau1p(bg,1:kink,shoc);

JTau1p1(kink+1)=NaN;
bgrid_plot1(kink+1)=bgrid_plot(kink)+0.0001;
EBGp1(kink+1)=EBGp(bg,kink,shoc);
Eq_bp1(kink+1)=Eq_bp(bg,kink,shoc);
Eq_bmax1(kink+1)=Eq_bmax(bg,kink,shoc);

bgrid_plot1(kink+2:end)=bgrid_plot(kink+1:end);
EBGp1(kink+2:end)=EBGp(bg,kink+1:end,shoc);
JTau1p1(kink+2:end)=JTau1p(bg,kink+1:end,shoc);
Eq_bmax1(kink+2:end)=Eq_bmax(bg,kink+1:end,shoc);
Eq_bp1(kink+2:end)=Eq_bp(bg,kink+1:end,shoc);




figure 
yyaxis right
plot(bgrid_plot1(:)/mY,squeeze(EBGp1(:))/mY,'Linewidth',2,'Color',[.75,.25,1],'LineStyle','-.'); hold on;
plot(bgrid_plot1(:)/mY,squeeze(Eq_bp1(:))/mY,'Linewidth',2,'Color','r','LineStyle','--'); hold on;
plot(bgrid_plot1(:)/mY,squeeze(Eq_bmax1(:))/mY,'Linewidth',2,'Color',[100 100 100]/255,'LineStyle',':'); hold on;
%plot(bgrid_plot1(:)/mY,bgrid_plot1(:)/mY,'Linewidth',2,'Color','k','LineStyle','-','Marker','none');hold on;legend boxoff;
ylim([0.1 .5]);
ylabel('End of period debt $B''$ and $L''$ ','Interpreter','Latex'); legend boxoff;
yyaxis left
plot(bgrid_plot1(:)/mY,squeeze(JTau1p1(:))*100,'Linewidth',3,'Color','b','LineStyle','-'); hold on;
%ylim([0 .1]);
ylabel('Optimal tax rate ','Interpreter','Latex'); legend boxoff;


xlabel('Current debt','Interpreter','Latex')
Hlegend=legend('Optimal Tax','Public debt','Private debt','Private debt limit','Location','northwest'); set(Hlegend,'Interpreter','Latex')
%Hlegend=legend('Baseline','Social planner','Debt limit','45 degree','Location','southeast'); set(Hlegend,'Interpreter','Latex')
hold off;xlim([0.2 .65]);
set(gca, 'FontSize',15)
exportfig(gcf,'Plots\Fig10A_Optimal_Tax_policy.eps','color','cmyk','resolution',1600)
%% OPTIMAL POLICY IF NO PUBLIC DEBT
mY=mean(YSIM_SP_NOGOV);

shoc=13;

%FOR THE KINK
kink=18;
bgrid_NOGOV1=zeros(NB+1,1);
EBGp1=zeros(NB+1,1);
bp2D1=zeros(NB+1,1);
bpmax2D1=zeros(NB+1,1);
tau_NoGov1=zeros(NB+1,1);

bgrid_NOGOV1(1:kink)=bgrid_NOGOV(1:kink);
bp2D1(1:kink)=bp2D(1:kink,shoc);
bpmax2D1(1:kink)=bpmax2D(1:kink,shoc);
tau_NoGov1(1:kink)=tau_NoGov(1:kink,shoc);

tau_NoGov1(kink+1)=NaN;
bgrid_NOGOV1(kink+1)=bgrid_NOGOV(kink)+0.0001;
bp2D1(kink+1)=bp2D(kink,shoc);
bpmax2D1(kink+1)=bpmax2D(kink,shoc);

bgrid_NOGOV1(kink+2:end)=bgrid_NOGOV(kink+1:end);
tau_NoGov1(kink+2:end)=tau_NoGov(kink+1:end,shoc);
bpmax2D1(kink+2:end)=bpmax2D(kink+1:end,shoc);
bp2D1(kink+2:end)=bp2D(kink+1:end,shoc);


figure
yyaxis left
plot(bgrid_NOGOV1(:)/mY,squeeze(tau_NoGov1(:)),'Linewidth',3,'Color','b','LineStyle','-'); hold on;
ylim([0 .1]);
ylabel('Optimal tax rate ','Interpreter','Latex'); legend boxoff;
yyaxis right
plot(bgrid_NOGOV1(:)/mY,squeeze(bp2D1(:))/mY,'Linewidth',2,'Color','r','LineStyle','--'); hold on;
plot(bgrid_NOGOV1(:)/mY,squeeze(bpmax2D1(:))/mY,'Linewidth',2,'Color',[100 100 100]/255,'LineStyle',':'); hold on;
%plot(bgrid_NOGOV1(:)/mY,bgrid_NOGOV1(:)/mY,'Linewidth',2,'Color','k','LineStyle','-','Marker','none');hold on;legend boxoff;
ylim([0.1 .5]);
ylabel('End of period private debt $B''$','Interpreter','Latex'); legend boxoff;
xlabel('Current debt','Interpreter','Latex')
Hlegend=legend('Optimal tax with no public debt','Private debt','Private debt limit','Location','northwest'); set(Hlegend,'Interpreter','Latex')
%Hlegend=legend('Baseline','Social planner','Debt limit','45 degree','Location','southeast'); set(Hlegend,'Interpreter','Latex')
hold off;xlim([0.1 .5]);
set(gca, 'FontSize',15)
exportfig(gcf,'Plots\Fig10B_Optimal_Tax_policy_nopublic.eps','color','cmyk','resolution',1600)